import { mapMutations, mapActions, mapState } from "vuex";
import _, { template } from "lodash";
import { typeClassName } from "@/pages/config/classNameMap";
export default {
  data() {
    return {
      vms: {}, //页面组件vue实例
      models: {
        //模型
        qualityItem: {
          ...typeClassName["qualityItem"], //质检检项
        },
        material: {
          ...typeClassName["material"], //原料
          //
        },
        materiaQuality: {
          ...typeClassName["materiaQuality"], //原料绑定质检项的容器
        },
      },

      dataSet: {
        //数据集
        qualityTreeData: [],
        qualityTreeCurrent: null,
        qualityTreeAllData: [], // 每个设备存储所有的选项hier
        qualityTreePoint: null,
        qualityTreeSelect: [],
        qualityTableData: [],
        rightSelectIds: [],
      },
      selfFilter: {
        bf_template: true,
      },
    };
  },
  provide() {
    return {
      anc: this,
    };
  },
  computed: {},
  created() {
    Object.keys(this.models).map((k) => {
      this.models[k].clz = this.$modelObject.create(this.models[k].modelType);
      this.models[k].clzs = this.$modelObject.create(
        this.models[k].modelType + "Collection"
      );
    });
  },
  mounted() {
    this.$nextTick(() => {
      this.qualityTreeData();

      if (process.env.NODE_ENV === "development") {
        console.log(this);
      }
    });
  },
  methods: {
    // 加载表格信息
    loadPage(
      pageNavigation,
      filter,
      hierValueCondition = "",
      orderBy = "",
      hiddenHierData,
      like = false
    ) {
      let newFilter = {
        ...filter,
        className: this.models.qualityItem.className,
        type: this.models.qualityItem.type,
        ...this.selfFilter,
      };

      this._$queryByPage(
        this.models["qualityItem"].clzs,
        pageNavigation,
        newFilter,
        hierValueCondition,
        orderBy,
        hiddenHierData
      ).then((res) => {
        this.dataSet.qualityTableData =
          this.models["qualityItem"].clzs.valueRecords;
        // this.vms.table.pageNavigation.total = this.models["qualityItem"].clzs.total;
        if (this.dataSet.qualityTreeCurrent) {
          this.qualityTreeSelect(this.dataSet.qualityTreeCurrent);
        }
      });
    },
    /**
     *  获取左侧树设备信息 qualityTreeData
     */
    qualityTreeData() {
      let filter = {
        className: this.models.material.className,
        type: this.models.material.type,
      };

      this.vms.tree.loading = true;
      this.models["material"].clzs.query(filter).then((flag) => {
        if (flag) {
          this.dataSet.qualityTreeData = this.models[
            "material"
          ].clzs.records.map((d) => {
            return {
              id: d.id,
              parentId: "root",
              name: d.values.sf_name,
              sf_name: d.values.sf_name,
              sf_code: d.values.sf_code,
            };
          });

          this.vms.tree.loading = false;
        } else {
          this.vms.tree.loading = false;
        }
      });
    },
    /**
     *
     * @param {*} item
     * 选择左侧设备方法
     */
    qualityTreeSelect(item) {
      this.dataSet.qualityTreeCurrent = item;
      this.$api.quality.queryMaterialTemById(item).then((res) => {
        this.dataSet.qualityTreePoint = res;
        this.dataSet.qualityTreeSelect = [];
        let parentId = this._$getHierItemField(
          this.dataSet.qualityTreePoint.hier,
          "质检项",
          "nodeId"
        );

        if (!parentId) {
          return;
        }
        this.dataSet.qualityTreeAllData =
          this.dataSet.qualityTreePoint.hier.content.filter(
            (item) => item.parentId == parentId
          ); // 进入页面点击设备存储所有的选项
        this.dataSet.qualityTreeSelect =
          this.dataSet.qualityTreeAllData.map((item) => item.link.linkId);

        let qualityTableData = this._clone(this.dataSet.qualityTableData);
        this.dataSet.rightSelectIds = [];
        qualityTableData.forEach((item) => {
          if (this.dataSet.qualityTreeSelect.includes(item.id)) {
            item._checked = true;
            this.dataSet.rightSelectIds.push(item.id);
           
          } else {
            item._checked = false;
          }
        });
        this.dataSet.qualityTableData = qualityTableData;
      });
    },

    /**
     *
     * @param {*} selection
     * @returns
     * 巡检项选中的数据
     */
    qualityTableSelect(selection) {
      let selIds = selection.map((item) => item.id);
      let delLinkIds = this.dataSet.rightSelectIds.filter(
        (id) => !selIds.includes(id)
      );
      let addLinkIds = selIds.filter(
        (id) => !this.dataSet.rightSelectIds.includes(id)
      );
      let parentId = this._$getHierItemField(
        this.dataSet.qualityTreePoint.hier,
        "质检项",
        "nodeId"
      );
      if (delLinkIds.length > 0) {
        let delIds = this.dataSet.qualityTreeAllData
          .filter((item) => delLinkIds.includes(item.link.linkId))
          .map((item) => item.id);
        this.$api.HierInstanceDeleteHier(delIds);
      }
      if (addLinkIds.length > 0) {
        let dataArray = addLinkIds.map((id) => {
          return {
            containerModel: this.models.materiaQuality.modelType,
            containerId: this.dataSet.qualityTreePoint.id,
            parentId: parentId,
            linkId: id,
            values: { linkId: id },
          };
        });
        this.$api.HierInstanceSaveHier(dataArray);
      }
    },
  },
};
